(function(u, d) {
    var l, n, p, q;
    l = 200;
    n = 400;
    p = 180;
    q = {
        title: "\u65e0\u803b\uff0c\u6211\u5c45\u7136{0}\uff0c\u624d{1}\u5206\uff0c\u51fb\u8d25{2}%\u4eba\uff01\u8fde\u65b9\u5757\u6e38\u620f\u90fd\u5728\u8650\u6211\uff01",
        link: "http://hs.mengzhijy.com/games/tetris",
        desc: "变态方块游戏，纯粹找虐",
        img_width: "640",
        img_height: "640",
        img_url: "http://hs.mengzhijy.com/tetris/cube.png"
    };
    var r = [[[0, -1], [ - 1, 1], [0, 0], [0, 1]], [[0, -1], [1, 1], [0, 0], [0, 1]], [[0, 0], [ - 1, 0], [0, 1], [0, -1]], [[ - 1, -1], [0, -1], [0, 0], [1, 0]], [[0, -1], [1, -1], [ - 1, 0], [0, 0]], [[0, -1], [1, -1], [0, 0], [1, 0]], [[0, -1], [0, 0], [0, 1], [0, 2]]],
        s = {
            "童年夭折": [0, 1],
            "毫无童年": [2, 3],
            "童年苦命": [4, 5],
            "童年辍学": [6, 8],
            "童年早熟": [9, 12],
            "童年巨乳": [13, 16],
            "童年老妖": [17, 20],
            "外星人": [21, Infinity]
        },
        k = {
            clone: function(a) {
                var b;
                if ("object" === typeof a) if ("[object Array]" === Object.prototype.toString.call(a)) {
                    b = [];
                    for (var c = a.length; c--;) b[c] = this.clone(a[c])
                } else for (c in b = {},
                    a) b[c] = this.clone(a[c]);
                else return a;
                return b
            },
            format: function() {
                for (var a = arguments[0], b = 1, c = arguments.length; b < c; b++) a = a.replace(RegExp("{(" + (b - 1) + ")}", "g"), arguments[b]);
                return a
            }
        },
        g,
        m,
        e,
        t = function() {
            var a = d.getElementsByTagName("canvas")[0];
            a.width = l = a.offsetWidth;
            a.height = n = 20 * parseInt(a.offsetHeight / 20);
            m = a.getContext("2d");
            this.score = this.nextShapeIndex = 0;
            this.isParse = !0;
            this.initElement();
            this.changeGrade(0);
            this.setStatistic();
            this.bindEvent()
        };
    t.prototype = {
        bindEvent: function() {
            function a(a) {
                switch (a.target.getAttribute("data-role")) {
                    case "left":
                        c = p;
                        g.translateXShape( - 1);
                        break;
                    case "right":
                        c = p,
                            g.translateXShape(1)
                }
            }
            var b, c;
            d.addEventListener("touchstart",
                function(e) {
                    g.isParse || (a(e), b && clearInterval(b), b = setInterval(function() {
                            a(e)
                        },
                        c))
                },
                !1);
            d.addEventListener("touchend",
                function() {
                    b && clearInterval(b)
                },
                !1);
            d.addEventListener("ontouchstart" in d ? "touchend": "click",
                function(a) {
                    switch (a.target.getAttribute("data-role")) {
                        case "up":
                            !g.isParse && g.rotateShape();
                            break;
                        case "start":
                            g.start()
                    }
                },
                !1);
            this.bindShareEvent()
        },
        bindShareEvent: function() {
            var a = function() {
                WeixinJSBridge && WeixinJSBridge.on("menu:share:timeline",
                    function() {
                        var a = k.clone(q);
                        a.title = k.format(a.title, g.level, g.score, g.getPercent());
                        WeixinJSBridge && WeixinJSBridge.invoke("shareTimeline", a,
                            function(a) {})
                    })
            };
            "undefined" === typeof WeixinJSBridge ? d.addEventListener ? d.addEventListener("WeixinJSBridgeReady", a, !1) : d.attachEvent && (d.attachEvent("WeixinJSBridgeReady", a), d.attachEvent("onWeixinJSBridgeReady", a)) : a()
        },
        changeGrade: function(a) {
            "undefined" === typeof a ? (this.waitTime -= 0.2 * this.waitTime, 10 >= this.waitTime && (this.waitTime = 10), this.score += 1) : this.score = a;
            this.scoreElem.innerHTML = k.format("S\uff1a{0}\u5206", this.score);
            for (var b in s) if (a = s[b], this.score >= a[0] && this.score <= a[1]) {
                this.level = b;
                this.levelElem.innerHTML = k.format("lv\uff1a{0}", b);
                break
            }
        },
        checkXBoundary: function(a) {
            var b = 0,
                c = this.transitionCoordinate(this.shape.data, this.shape.x, this.shape.y);
            try {
                for (var d = 0,
                         h = c.length; d < h; d++) {
                    var f = c[d][0] / 20,
                        g = c[d][1] / 20;
                    if ("undefined" === typeof a && 0 > f || -1 === a && 0 === f) {
                        b = -1;
                        break
                    } else if ("undefined" === typeof a && f > e[0].length - 1 || 1 === a && f === e[0].length - 1) {
                        b = 1;
                        break
                    } else if ("undefined" === typeof a && e[g][f] || -1 === a && e[g][f - 1] || 1 === a && e[g][f + 1]) {
                        b = 2;
                        break
                    }
                }
            } catch(k) {}
            return b
        },
        checkYBoundary: function() {
            for (var a = !0,
                     b = this.transitionCoordinate(this.shape.data, this.shape.x, this.shape.y), c = 0, d = b.length; c < d; c++) {
                var h = b[c][0] / 20,
                    f = b[c][1] / 20;
                if (f === e.length - 1) {
                    a = !1;
                    break
                }
                if ("undefined" !== typeof e[f + 1] && e[f + 1][h]) { - 1 === f && this.stop();
                    a = !1;
                    break
                }
            }
            return a
        },
        clearShape: function() {
            for (var a = this.transitionCoordinate(this.shape.data, this.shape.x, this.shape.y), b = 0, c = a.length; b < c; b++) m.clearRect(a[b][0], a[b][1], 20, 20)
        },
        createMatrix: function() {
            e = [];
            for (var a = 0,
                     b = n / 20; a < b; a++) {
                e[a] = [];
                for (var c = 0,
                         d = l / 20; c < d; c++) e[a][c] = !1
            }
        },
        createNewShape: function() {
            this.shape = {
                x: l / 2 - 40,
                y: -40,
                index: this.nextShapeIndex,
                data: k.clone(r[this.nextShapeIndex])
            };
            this.nextShapeIndex = parseInt(Math.random() * r.length);
            this.shapeElem.className = "main-info-shape main-info-shape" + this.nextShapeIndex;
            this.translateYShape()
        },
        drawShape: function() {
            for (var a = this.transitionCoordinate(this.shape.data, this.shape.x, this.shape.y), b = 0, c = a.length; b < c; b++) m.drawImage(this.rectImage, a[b][0], a[b][1], 20, 20)
        },
        drawWall: function() {
            var a = this.transitionCoordinate(this.shape.data, this.shape.x, this.shape.y);
            try {
                for (var b = 0,
                         c = a.length; b < c; b++) e[a[b][1] / 20][a[b][0] / 20] = !0
            } catch(d) {
                g.stop()
            }
            a = 0;
            for (b = e.length; a < b; a++) {
                for (var h = !0,
                         c = e[a].length, f = 0; f < c; f++) if (!e[a][f]) {
                    h = !1;
                    break
                }
                if (h) for (this.changeGrade(), h = a; 0 < h; h--) for (f = 0; f < c; f++) e[h][f] = e[h - 1][f]
            }
            m.clearRect(0, 0, l, n);
            a = 0;
            for (b = e.length; a < b; a++) for (c = 0, h = e[a].length; c < h; c++) e[a][c] && m.drawImage(this.rectImage, 20 * c, 20 * a, 20, 20)
        },
        getPercent: function() {
            var a = 55;
            0 < this.score && (a += this.score + 20);
            return 99 < a ? 99 : a
        },
        getRedeemCode: function() {
            var a = [],
                b = 0;
            for (a[0] = [1, 3, 5, 7, 9][parseInt(5 * Math.random())]; b !== 36 - a[0];) for (var b = 0,
                                                                                                 c = 1; 6 > c; c++) a[c] = parseInt(10 * Math.random()),
                b += a[c];
            return a.join("")
        },
        initElement: function() {
            this.shapeElem = d.getElementsByClassName("main-info-shape")[0];
            this.scoreElem = d.getElementsByClassName("main-info-score")[0];
            this.levelElem = d.getElementsByClassName("main-info-level")[0];
            this.curtainElem = d.getElementsByClassName("curtain")[0];
            this.startElem = d.getElementsByClassName("curtain-start")[0];
            this.endElem = d.getElementsByClassName("curtain-end")[0];
            this.resultElem = this.endElem.getElementsByClassName("curtain-header")[0];
            this.statisticElem = d.getElementsByClassName("curtain-info")[0];
            this.rectImage = new Image;
            this.rectImage.src = "/tetris/rect.png"
        },
        pause: function() {
            this.isParse ? (this.isParse = !1, this.translateYShape()) : (this.isParse = !0, this.translateTimer && clearTimeout(this.translateTimer))
        },
        rotateShape: function() {
            if (5 !== this.shape.index) {
                var a = this.shape.data,
                    b = [];
                this.clearShape();
                for (var c = 0,
                         d = a.length; c < d; c++) b.push([a[c][1], -a[c][0]]);
                this.shape.data = b;
                for (this.drawShape(); 1 === Math.abs(this.checkXBoundary());) this.translateXShape( - this.checkXBoundary())
            }
        },
        setStatistic: function() {
            var a = new XMLHttpRequest;
            a.onreadystatechange = function() {
                if (4 === a.readyState && 200 === a.status) {
                    var b = JSON.parse(a.responseText);
                    g.statisticElem.innerHTML = k.format('<span style="display: none;">\u53c2\u4e0e\u4eba\u6570\uff1a{0}\u4eba</span><span class="curtain-info-right">公众号：华山旅游故事会</span>', b.count)
                }
            };
            a.open("GET", "/api/tetris", !0);
            a.send()
        },
        start: function() {
            this.curtainElem.style.display = "none";
            this.waitTime = 180;
            this.isParse = !1;
            this.changeGrade(0);
            this.createMatrix();
            m.clearRect(0, 0, l, n);
            this.createNewShape()
        },
        stop: function() {
            this.isParse = !0;
            this.translateTimer && clearTimeout(this.translateTimer);
            this.changeGrade(this.score);
            this.curtainElem.style.display = "block";
            this.startElem.style.display = "none";
            this.endElem.style.display = "block";
            var a = k.format("<p>\u606d\u559c\u4f60\u83b7\u5f97{0}\u5206\uff0c\u8d85\u8fc7{1}%\u7684\u4eba</p><p>\u83b7\u5f97\u201c{2}\u201d\u52cb\u7ae0</p>", this.score, this.getPercent(), this.level);
            21 <= this.score && (a += "<p>\u5151\u6362\u7801\uff1a" + this.getRedeemCode() + "</p>");
            this.resultElem.innerHTML = a
        },
        transitionCoordinate: function(a, b, c) {
            for (var d = [], e = 0, f = a.length; e < f; e++) d.push([20 * a[e][0] + b, 20 * a[e][1] + c]);
            return d
        },
        translateXShape: function(a) {
            0 === this.checkXBoundary(a) && (this.clearShape(), this.shape.x += 20 * a, this.drawShape())
        },
        translateYShape: function() {
            this.translateTimer && clearTimeout(this.translateTimer);
            this.isParse || (this.checkYBoundary() ? (this.clearShape(), this.shape.y += 20, this.drawShape(), this.translateTimer = setTimeout(function() {
                        g.translateYShape()
                    },
                    this.waitTime)) : (this.translateTimer && clearTimeout(this.translateTimer), this.drawWall(), this.createNewShape()))
        }
    };
    g = new t
})(window, document);